﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// .net core处理请求类
    /// </summary>
    public class UniAppListHelper
    {

        /// <summary>
        /// 根据表名创建Index管理页面
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateUniAppListClass(string strFilePath,string strProjectName, string strTableName,string strTableComment,string strDBName)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            Directory.CreateDirectory(strFilePath);
            //为每个表创建一个独立目录存放
            string strTableNamePath = strFilePath + "\\"+ strClassName;
            if (!Directory.Exists(strTableNamePath))
            {
                Directory.CreateDirectory(strTableNamePath);
            }
            StreamWriter sw = new StreamWriter(strTableNamePath + "\\list.vue", false, Encoding.GetEncoding("utf-8"));//生成物理文件
            string strTableComment2 = CommonHelper.GetColumnKeyComment(strTableComment);

            sw.WriteLine("<template>");
            sw.WriteLine("	<view class=\"content\">");
            sw.WriteLine("		<view class=\"item_list u-m-t-20 u-m-b-20\">");
            sw.WriteLine("			<view class=\"item_cols u-skeleton\">");
            sw.WriteLine("				<view class=\"u-p-t-20\" v-for=\"(item, index) in data_list\" :key=\"index\">");
            sw.WriteLine("					<u-row gutter=\"16\" @click=\"toDetails(item)\">");
            sw.WriteLine("						<u-col span=\"4\">");
            sw.WriteLine("							<view class=\"img u-skeleton-rect\">");
            sw.WriteLine("								<!-- 显示图片 -->");
            sw.WriteLine("								<u-image width=\"100%\" height=\"100%\" :border-radius=\"10\" :src=\"item.oss_path\"></u-image>");
            sw.WriteLine("							</view>");
            sw.WriteLine("						</u-col>");
            sw.WriteLine("						<u-col span=\"8\">");
            sw.WriteLine("							<view>");
            sw.WriteLine("								<!-- 显示标题 -->");
            sw.WriteLine("								<view class=\"u-line-2 u-main-color u-skeleton-fillet\">");
            sw.WriteLine("									<text v-if=\"loading\">loading</text>");
            sw.WriteLine("									<text v-else>{{item." + strTableNameLower + "_title}}</text>");
            sw.WriteLine("								</view>");
            sw.WriteLine("								<view class=\"u-font-12 u-text-left u-m-t-20 u-skeleton-fillet\">");
            sw.WriteLine("									<text style=\"color: #999;\">");
            sw.WriteLine("										<text v-if=\"loading\">loading</text>");
            sw.WriteLine("										<text v-else>{{item.created_at}}</text>");
            sw.WriteLine("									</text>");
            sw.WriteLine("								</view>");
            sw.WriteLine("							</view>");
            sw.WriteLine("						</u-col>");
            sw.WriteLine("					</u-row>");
            sw.WriteLine("					<view class=\"u-p-t-20\" v-if=\"index + 1 != data_list.length\">");
            sw.WriteLine("						<u-line color=\"#f2f2f2\" />");
            sw.WriteLine("					</view>");
            sw.WriteLine("				</view>");
            sw.WriteLine("			</view>");
            sw.WriteLine("			<view class=\"u-m-t-40\" v-if=\"count>=(page*limit)+1\">");
            sw.WriteLine("				<u-loadmore :status=\"status\" />");
            sw.WriteLine("			</view>");
            sw.WriteLine("		</view>");
            sw.WriteLine("		<!-- 骨架屏组件 -->");
            sw.WriteLine("		<u-skeleton :loading=\"loading\" :animation=\"true\" bgColor=\"#fff\"></u-skeleton>");
            sw.WriteLine("		<!-- 无网络提示组件 -->");
            sw.WriteLine("		<u-no-network></u-no-network>");
            sw.WriteLine("	</view>");
            sw.WriteLine("</template>");
            sw.WriteLine("");
            sw.WriteLine("<script>");
            sw.WriteLine("	export default {");
            sw.WriteLine("		components: {");
            sw.WriteLine("");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		computed: {");
            sw.WriteLine("");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		data() {");
            sw.WriteLine("			return {");
            sw.WriteLine("				loading: true,");
            sw.WriteLine("				status: 'loadmore',");
            sw.WriteLine("				page: 1,");
            sw.WriteLine("				limit: 10,");
            sw.WriteLine("				data_list: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], //默认填充模拟数据，显示骨架屏效果，");
            sw.WriteLine("				count: 0,");
            sw.WriteLine("			}");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		onShow() {");
            sw.WriteLine("");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		onLoad() {");
            sw.WriteLine("			//默认加载数据");
            sw.WriteLine("			this.dataList();");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		/**");
            sw.WriteLine("		 * 下拉到底部加载更多");
            sw.WriteLine("		 */");
            sw.WriteLine("		onReachBottom() {");
            sw.WriteLine("			if (this.page * this.limit >= this.count) {");
            sw.WriteLine("				return;");
            sw.WriteLine("			}");
            sw.WriteLine("			this.status = 'loading';");
            sw.WriteLine("			this.page = ++this.page;");
            sw.WriteLine("");
            sw.WriteLine("			this.$u.get('/api/"+ strTableNameLower + "', {");
            sw.WriteLine("				page: this.page,");
            sw.WriteLine("				limit: this.limit,");
            sw.WriteLine("				//type: 1");
            sw.WriteLine("			}).then(res => {");
            sw.WriteLine("				if (res.code == 0) {");
            sw.WriteLine("					if (res.data) {");
            sw.WriteLine("						this.data_list = [...this.data_list, ...res.data]");
            sw.WriteLine("						if (this.page * this.limit >= this.count) {");
            sw.WriteLine("							this.status = 'nomore'");
            sw.WriteLine("						} else {");
            sw.WriteLine("							this.nomore = 'loadmore'");
            sw.WriteLine("						}");
            sw.WriteLine("					}");
            sw.WriteLine("				}");
            sw.WriteLine("			})");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		methods: {");
            sw.WriteLine("			/**");
            sw.WriteLine("			 * 获取列表数据");
            sw.WriteLine("			 */");
            sw.WriteLine("			dataList() {");
            sw.WriteLine("				this.$u.get('/api/" + strTableNameLower + "', {");
            sw.WriteLine("					page: this.page,");
            sw.WriteLine("					limit: this.limit,");
            sw.WriteLine("				    //type: 1");
            sw.WriteLine("				}).then(res => {");
            sw.WriteLine("					if (res.code == 0) {");
            sw.WriteLine("						// console.log('data='+JSON.stringify(res.data));");
            sw.WriteLine("						if (res.data) {");
            sw.WriteLine("							this.data_list = res.data");
            sw.WriteLine("							this.count = res.count");
            sw.WriteLine("							if (this.page * this.limit >= this.count) {");
            sw.WriteLine("								this.status = 'nomore'");
            sw.WriteLine("							}");
            sw.WriteLine("							this.loading = false;");
            sw.WriteLine("						}");
            sw.WriteLine("					}");
            sw.WriteLine("				})");
            sw.WriteLine("			},");
            sw.WriteLine("");
            sw.WriteLine("			/** ");
            sw.WriteLine("			 * 跳转到详情页面");
            sw.WriteLine("			 * @param {Object} obj");
            sw.WriteLine("			 */");
            sw.WriteLine("			toDetails(item) {");
            sw.WriteLine("				this.$u.route({");
            sw.WriteLine("					url: 'pages/" + strTableNameLower + "/details',");
            sw.WriteLine("					params: {");
            sw.WriteLine("						" + strPrimaryKey + ": item." + strPrimaryKey);
            sw.WriteLine("					}");
            sw.WriteLine("				});");
            sw.WriteLine("			}");
            sw.WriteLine("		},");
            sw.WriteLine("");
            sw.WriteLine("		/**");
            sw.WriteLine("		 * 分享");
            sw.WriteLine("		 */");
            sw.WriteLine("		onShareAppMessage() {");
            sw.WriteLine("");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("</script>");
            sw.WriteLine("");
            sw.WriteLine("<style scoped lang=\"scss\">");
            sw.WriteLine("	.content {");
            sw.WriteLine("		padding-bottom: 30rpx;");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	.item_list {");
            sw.WriteLine("		background-color: #ffffff;");
            sw.WriteLine("		width: 100%;");
            sw.WriteLine("		margin: auto;");
            sw.WriteLine("");
            sw.WriteLine("		.item_cols {");
            sw.WriteLine("			width: 98%;");
            sw.WriteLine("			margin: auto;");
            sw.WriteLine("");
            sw.WriteLine("			.img {");
            sw.WriteLine("				width: 220rpx;");
            sw.WriteLine("				height: 135rpx;");
            sw.WriteLine("");
            sw.WriteLine("				image {");
            sw.WriteLine("					width: 100%;");
            sw.WriteLine("					height: 100%;");
            sw.WriteLine("				}");
            sw.WriteLine("			}");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("</style>");

            sw.Close();

        }

    }
}
